<template>
  <div>
    <h3 class="h3-title">
      TA的动态&nbsp;&nbsp;&nbsp;({{ singerDetails.profile.eventCount }})
    </h3>
    <div class="layout-flex">
      <div v-if="singerDetails.profile.eventCount > 0" class="eventsList">
        <!-- v-loading="eventsListLoding" :element-loading-background="ELEMENT_LOADING_BACKGROUND" element-loading-spinner="el-icon-loading"  :element-loading-text="ELEMENT_LOADING_TEXT" -->
        <div class="item" v-for="(item, index) in eventsList" :key="index">
          <div class="user-img" @click="screenUserPage(item)">
            <img :src="item.user.avatarUrl" alt="" />
          </div>
          <div class="user-info">
            <div class="title">
              <b @click="screenUserPage(item)">{{ item.user.nickname }}</b>
              <span>{{ item.type | filterType }}</span>
              <div
                v-if="$route.query.id == userInfo.profile.userId"
                class="iconfont delete"
              >
                &#xe63c;
              </div>
              <div
                v-if="$route.query.id == userInfo.profile.userId"
                @click="deleteDynamic(item)"
                class="pl"
              >
                <i class="iconfont">&#xe609;</i>
                <span>删除</span>
              </div>
            </div>
            <div class="date">
              <span>
                {{ item.eventTime | dateFilter }}
              </span>
            </div>
            <div class="info">
              {{ item.json | jsonFilter }}
            </div>
            <div class="music" v-if="isItemJsonComputed(item.json)">
              <div class="img">
                <img :src="item.json | jsonCoverFilter" alt="" />
                <i class="iconfont">&#xe638;</i>
              </div>
              <div class="details">
                <div class="title">{{ item.json | jsonNameFilter }}</div>
                <div class="auth">{{ item.json | jsonAuthFilter }}</div>
              </div>
            </div>
            <div class="pic" v-if="item.pics.length > 0">
              <!-- <el-image 
               :style="{width:'100px'}"
                :src="item.pics[0].originUrl" 
                :preview-src-list="[item.pics[0].originUrl]">
              </el-image> -->
              <img
                :width="item.pics[0].originUrl | imgWFilter"
                :src="item.pics[0].originUrl"
                alt=""
              />
            </div>
            <div class="footer-control">
              <span class="icon-span-box"
                ><i
                  class="iconfont"
                  :style="{
                    color: item.info.liked ? 'rgb(225,59,59)' : '#0c73c2',
                  }"
                  >&#xe63b;</i
                ><span class="icspan">{{
                  item.info.commentThread.likedCount
                }}</span></span
              >
              <b>|</b>
              <span @click="transmitShare(item)"
                >转发 {{ item.insiteForwardCount }}</span
              >
              <b>|</b>
              <span @click="transmitDiscuss(item)"
                >评论 {{ item.info.commentThread.commentCount }}</span
              >
            </div>
          </div>
        </div>
        <div class="more" @click="getDynamic" v-if="more">加载更多</div>
      </div>
      <div v-if="singerDetails.profile.eventCount < 1" class="noList">
        <i class="img"></i>
        <span>还没有动态</span>
      </div>
      <div class="jointly">
        <ArtistList />
      </div>
    </div>
    <Transmit
      v-if="isTransmitDialog"
      :isTransmitDialog.sync="isTransmitDialog"
      :evId="evId"
      @init="init"
    />
  </div>
</template>

<script>
import Transmit from "@/components/transmit";
import $moment from "moment";
import { mapGetters } from "vuex";
import { userEvent, eventDel, commentEvent } from "@/api";
import ArtistList from "@/views/recommend/ArtistList";

export default {
  name: "dynamic",
  components: {
    Transmit,
    ArtistList,
  },
  data() {
    return {
      more: true,
      lasttime: "",
      evId: "",
      isTransmitDialog: false,
      eventsListLoding: false,
      eventsList: [
        // {
        //   eventTime: new Date(),
        //   user: {
        //     avatarUrl: "",
        //   },
        //   json: {
        //     msg: "",
        //     song: {
        //       name: "",
        //     },
        //   },
        //   insiteForwardCount: 0,
        //   info: {
        //     liked: false,
        //     commentThread: {
        //       likedCount: 0,
        //       commentCount: 0,
        //     },
        //   },
        //   user: {
        //     nickname: "",
        //   },
        //   type: 0,
        //   pics: [
        //     {
        //       originUrl: "",
        //     },
        //   ],
        // },
        // {
        //   // 时间
        //   eventTime: 1601895110301,
        //   user: {
        //     // 头像
        //     avatarUrl:
        //       "http://p1.music.126.net/pRDRt-ChX4Qg5FyMwTGXKg==/109951164440873447.jpg",
        //   },
        //   json: {
        //     // 详情
        //     msg:
        //       "听着很舒服的一首曲子，全曲以温柔的钢琴切入，循序渐进，融合进电声的效果，层次丰富，收尾也很妙。",
        //   },
        //   // 转发
        //   insiteForwardCount: 35,
        //   info: {
        //     // 是否点赞
        //     liked:false,
        //     commentThread: {
        //       // 点赞
        //       likedCount: 2248,
        //       // 评论
        //       commentCount: 292,
        //     },
        //   },
        //   // 名称
        //   nickname: "网易CEO丁磊",

        //   type: 18,
        //   /*
        //       18 分享单曲
        //       19 分享专辑
        //       17、28 分享电台节目
        //       22 转发
        //       39 发布视频
        //       35、13 分享歌单
        //       24 分享专栏文章
        //       41、21 分享视频
        //     */
        // },
      ],
    };
  },
  computed: {
    ...mapGetters(["singerDetails", "userInfo"]),
  },
  filters: {
    imgWFilter: function (val) {
      let img = new Image();
      img.src = val;
      let width = 190;
      if (img.height > img.width) {
        width = 190;
      } else {
        width = 224;
      }
      return width;
    },
    jsonCoverFilter: function (val) {
      try {
        return JSON.parse(val).song.img80x80;
      } catch (error) {}
    },
    jsonAuthFilter: function (val) {
      try {
        return JSON.parse(val)
          .song.artists.map((item) => item.name)
          .join("/");
      } catch (error) {}
    },
    jsonNameFilter: function (val) {
      try {
        return JSON.parse(val).song.name;
      } catch (error) {}
    },
    jsonFilter: function (val) {
      try {
        return JSON.parse(val).msg;
      } catch (error) {}
    },
    dateFilter: function (val) {
      return $moment(val).format("YYYY-MM-DD");
    },
    filterType: function (val) {
      switch (val) {
        case 18:
          return "分享单曲";
          break;
        case 19:
          return "分享专辑";
          break;
        case 17:
          return "分享电台节目";
          break;
        case 28:
          return "分享电台节目";
          break;
        case 22:
          return "转发";
          break;
        case 39:
          return "发布视频";
          break;
        case 35:
          return "分享歌单";
          break;
        case 13:
          return "分享歌单";
          break;
        case 24:
          return "分享专栏文章";
          break;
        case 41:
          return "分享视频";
          break;
        case 21:
          return "分享视频";
          break;
        default:
          return "分享单曲";
          break;
      }
    },
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.getDynamic();
    },
    getDynamic() {
      this.eventsListLoding = true;
      userEvent({
        uid: this.$route.query.id,
        // this.singerDetails.profile.eventCount\
        lasttime:this.lasttime,
        limit: 30,
      }).then((data) => {
        this.eventsList = this.eventsList.concat(data.events);
        this.eventsListLoding = false;
        this.lasttime = data.lasttime;
        this.more = data.more;
      });
    },
    screenUserPage(val) {
      const { userId } = val.user;
      this.$router.push({
        name: "userpage",
        query: {
          id: userId,
        },
      });
    },
    isItemJsonComputed(val) {
      try {
        return JSON.parse(val).song.img80x80;
      } catch (error) {}
    },
    transmitShare(val) {
      this.evId = val.id;
      this.isTransmitDialog = true;
    },
    deleteDynamic(val) {
      const { id: evId } = val;
      eventDel({ evId }).then((data) => {
        this.eventsList = this.eventsList.filter((item) => item.id !== evId);
        this.$Notification("删除成功", "success");
      });
    },
    transmitDiscuss(val) {
      console.log(val, "val");
      commentEvent({ threadId: val.id }).then((data) => {
        console.log(data, "评论");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.more {
  margin-top:20px;
  text-align: center;
  font-size: 12px;
  color: #999;
  cursor: pointer;
}
.noList {
  flex: 1;
  font-weight: 600;
  height: 359px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  font-family: "Microsoft Yahei", Arial, Helvetica, sans-serif;
  span {
    margin-left: 10px;
  }
  .img {
    background-image: url(../../assets/img/myHomepagefans.png);
    background-position: no-repeat 0 9999px;
    width: 64px;
    height: 50px;
    background-position: 0 -347px;
  }
}
.item {
  &:first-child {
    padding-top: 0;
  }
  display: flex;
  padding: 20px 0;
  border-bottom: 1px solid #e8e8e9;
  .music {
    margin: 10px 0;
    padding: 0 10px;
    background-color: rgb(245, 245, 245);
    height: 60px;
    display: flex;
    align-items: center;
    .details {
      font-size: 14px;
      .title {
        color: #333;
      }
      .auth {
        color: #666;
        font-size: 13px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 450px;
      }
    }
    .img {
      margin-right: 10px;
      width: 40px;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
      img {
        width: 100%;
      }
      .iconfont {
        font-size: 20px;
        line-height: 1;
        color: #efefef;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        width: 20px;
        height: 20px;
        cursor: pointer;
      }
    }
  }
}
.eventsList {
  border-right: 1px solid rgb(212, 212, 212);
  flex: 1;
  padding: 20px 0;
  padding-right: 25px;
  border-top: 1px solid #e8e8e9;
  display: flex;
  flex-direction: column;
  min-height: 300px;
  .footer-control {
    display: flex;
    justify-content: flex-end;
    font-size: 12px;
    .iconfont {
      display: inline-block;
      font-size: 16px;
      vertical-align: middle;
    }
    .icspan {
      vertical-align: middle;
    }
    .icon-span-box {
      transform: translateY(-2px);
    }
    & > span {
      display: inline-block;
      color: #0c73c2;
      cursor: pointer;
      &:hover {
        text-decoration: underline;
      }
    }
    b {
      color: #c7c7c7;
      margin: 0 12px;
      font-weight: 500;
    }
  }
  .user-info {
    flex: 1;
    .delete {
      float: right;
      cursor: pointer;
      font-size: 10px;
    }
    .delete:hover + div {
      display: block;
    }
    .pl {
      cursor: pointer;
      background-color: #fff;
      &:hover {
        display: block;
        background-color: rgb(243, 243, 243);
      }
      &::before {
        content: "";
        position: absolute;
        height: 10px;
        top: -10px;
        left: 0;
        right: 0;
      }
      display: none;
      position: absolute;
      right: 0;
      width: 105px;
      height: 30px;
      color: #666;
      border: 1px solid #cccfd9;
      box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
      // text-align: center;
      .iconfont {
        margin-left: 4px;
        font-size: 20px;
        vertical-align: middle;
        color: rgb(201, 201, 201);
      }
      span {
        vertical-align: middle;
        margin-left: 6px;
        font-size: 12px;
      }
    }
    .info {
      width: 500px;
      overflow: hidden;
      font-size: 14px;
      color: #333;
    }
    .date {
      span {
        color: #999;
        cursor: pointer;
        font-size: 12px;
        &:hover {
          text-decoration: underline;
        }
      }
    }
    .title {
      position: relative;
      font-size: 14px;
      b {
        font-weight: 500;
        cursor: pointer;
        color: #0c73c2;
        &:hover {
          text-decoration: underline;
        }
        margin-right: 6px;
      }
      span {
        color: #666;
      }
    }
  }
  .user-img {
    cursor: pointer;
    width: 45px;
    height: 45px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 15px;
    img {
      width: 100%;
    }
  }
}
.h3-title {
  font-size: 20px;
  color: #333;
  border-bottom: 2px solid #c20c0c;
}
.layout-flex {
  position: relative;
  display: flex;
  // &::after {
  //   content: "";
  //   position: absolute;
  //   width: 1px;
  //   background-color: rgb(212, 212, 212);
  //   right: 250px;
  //   top: 0;
  //   bottom: -40px;
  // }
  .jointly {
    width: 300px;
  }
}
</style>